package br.usp.ime.dojo.core.repositories.impl.memoria;

import java.util.ArrayList;
import java.util.List;

import br.usp.ime.dojo.core.entities.User;
import br.usp.ime.dojo.core.exceptions.DuplicatedUserException;
import br.usp.ime.dojo.core.repositories.UserRepository;

public class UserRepositoryInMemory implements UserRepository {

	private List<User> users = new ArrayList<User>();
	
	private static UserRepositoryInMemory instance = null;
	
	public static UserRepositoryInMemory getInstance() {
		if (instance == null) {
			instance = new UserRepositoryInMemory();
		}
		return instance;
	}
	private UserRepositoryInMemory() {}
	
	public void add(User u) throws DuplicatedUserException {
		if (get(u.getName()) != null) throw new DuplicatedUserException();
		users.add(u);
	}

	public User get(String name) {
		for (User user : users) {
			if (user.getName().equals(name)) {
				return user;
			}
		}
		return null;
	}

	public void clearAll() {
		users.clear();
	}

	public int count() {
		return users.size();
	}

}
